Imports System.Net.Mail

Partial Class Add_User
    Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Country_cmb.Items.Count < 1 Then
            Dim zClass As New LookUp.General_Lookup
            Country_cmb.Items.Clear()
            zClass.Loader(Country_cmb, "COUNTRY", "country_desc", "country_cd")
            Country_cmb.Items.Insert(0, "")

            Dept_cmb.Items.Clear()
            zClass.Loader(Dept_cmb, "DEPARTMENT", "Desc", "ID")
            Dept_cmb.Items.Insert(0, "")

            Level_cmb.Items.Clear()
            zClass.Loader(Level_cmb, "ADVISOR_LEVEL", "Desc", "ID")
            Level_cmb.Items.Insert(0, "")

            If Session("user_mode") = "View" Then
                LoadDetails()
                DisableMost()
            ElseIf Session("user_mode") = "Edit" Then
                LoadDetails()
                Name_txt.Enabled = False
            End If

            If Advisor_chk.Checked Then
                Level_cmb.Enabled = True
            Else
                Level_cmb.Enabled = False
            End If

        End If
    Call SecurePage()
    End Sub

    Protected Sub Country_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Country_cmb.SelectedIndexChanged
        If Country_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        City_cmb.Items.Clear()
        zClass.Loader(City_cmb, "CITY", "city_desc", "city_cd", Country_cmb.SelectedValue)
        City_cmb.Items.Insert(0, "")

    End Sub

    Protected Sub Dept_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Dept_cmb.SelectedIndexChanged
        If Dept_cmb.SelectedItem.Text = "" Then Exit Sub

        Dim zClass As New LookUp.General_Lookup
        Role_cmb.Items.Clear()
        zClass.Loader(Role_cmb, "ROLE", "role_desc", "role_cd", Dept_cmb.SelectedValue)
        Role_cmb.Items.Insert(0, "")

    End Sub

    Private Sub DisableMost()
        'fix this
        Name_txt.Enabled = False
        Save_btn.Enabled = False
        LastName_txt.Enabled = False
        FirstName_txt.Enabled = False
        MiddleName_txt.Enabled = False
        Dept_cmb.Enabled = False
        Role_cmb.Enabled = False
        Advisor_chk.Enabled = False
        Level_cmb.Enabled = False
        Add1_txt.Enabled = False : Add2_txt.Enabled = False
        City_cmb.Enabled = False : Country_cmb.Enabled = False : Zip_txt.Enabled = False
        T1_txt.Enabled = False : M1_txt.Enabled = False : F1_txt.Enabled = False
        Email_txt.Enabled = False : Remarks_txt.Enabled = False
    End Sub

    Private Sub LoadDetails()
        Dim sConn As New Data.SqlClient.SqlConnection
        Dim sCom As New Data.SqlClient.SqlCommand
        Dim sDApt As New Data.SqlClient.SqlDataAdapter(sCom)
        Dim sDT As New Data.DataTable
        Dim zClass As New LookUp.General_Lookup

        Try
            sConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("CnnStr")
            sCom.CommandType = Data.CommandType.Text
            sCom.Connection = sConn
            'fix this
            sCom.CommandText = "SELECT * FROM user_mstr WHERE user_name='" & Session("user_cd") & "'"
            sConn.Open()
            sDApt.Fill(sDT)

            With sDT.Rows(0)

                Name_txt.Text = .Item(0).ToString
                LastName_txt.Text = .Item(2).ToString
                FirstName_txt.Text = .Item(3).ToString
                MiddleName_txt.Text = .Item(4).ToString
                Dept_cmb.SelectedItem.Selected = False
                If Trim(.Item(5).ToString) <> "" Then Dept_cmb.Items.FindByValue(.Item(5).ToString).Selected = True
                Role_cmb.Items.Clear()
                zClass.Loader(Role_cmb, "ROLE", "role_desc", "role_cd", Dept_cmb.SelectedValue)
                Role_cmb.Items.Insert(0, "")
                If Trim(.Item(6).ToString) <> "" Then Role_cmb.Items.FindByValue(.Item(6).ToString).Selected = True

                Advisor_chk.Checked = .Item(7).ToString

                Level_cmb.SelectedItem.Selected = False
                If Trim(.Item(8).ToString) <> "" Then Level_cmb.Items.FindByValue(.Item(8).ToString).Selected = True

                Add1_txt.Text = .Item(9).ToString
                Add2_txt.Text = .Item(10).ToString

                Country_cmb.SelectedItem.Selected = False
                If Trim(.Item(12).ToString) <> "" Then Country_cmb.Items.FindByValue(.Item(12).ToString).Selected = True

                City_cmb.Items.Clear()
                zClass.Loader(City_cmb, "CITY", "city_desc", "city_cd", Country_cmb.SelectedValue)
                City_cmb.Items.Insert(0, "")
                If Trim(.Item(11).ToString) <> "" Then City_cmb.Items.FindByValue(.Item(11).ToString).Selected = True

                Zip_txt.Text = .Item(13).ToString
                T1_txt.Text = .Item(14).ToString
                M1_txt.Text = .Item(16).ToString
                F1_txt.Text = .Item(15).ToString
                Email_txt.Text = .Item(17).ToString
                Remarks_txt.Text = .Item(18).ToString

            End With

        Catch ex As Exception

        Finally
            sConn.Close()
        End Try

    End Sub

    Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
        Session.Remove("user_mode") : Session.Remove("user_cd")
        Response.Redirect("~/Admin/System_Users.aspx")
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim zClass As New LookUp.General_Lookup
        Dim dm As New DAOMain.CommonDAO
        Dim strTemp As String = ""

        If Me.Advisor_chk.Checked Then
            If Me.Level_cmb.SelectedValue.ToString = "" Then
                Dim pFunc As New PolyFunc.ASPFunc

                pFunc.MessageAlert(Me, "Advisor Level Required")
                pFunc = Nothing
            End If
        End If

        If Session("user_mode") = "Edit" Then
            'fix this
            zClass.ExecQuery("UPDATE user_mstr SET " & _
                "user_last_name=" & UCase(dm.Q(Trim(LastName_txt.Text))) & _
                ", user_first_name=" & UCase(dm.Q(Trim(FirstName_txt.Text))) & _
                ", user_middle_name=" & UCase(dm.Q(Trim(MiddleName_txt.Text))) & _
                ", dept_cd=" & UCase(dm.Q(Trim(Dept_cmb.SelectedValue))) & _
                ", role_cd=" & UCase(dm.Q(Trim(Role_cmb.SelectedValue))) & _
                ", advisor_tag=" & CInt(Advisor_chk.Checked) & _
                ", advisor_level_cd=" & UCase(dm.Q(Trim(Level_cmb.SelectedValue))) & _
                ", user_address=" & UCase(dm.Q(Trim(Add1_txt.Text))) & _
                ", user_address2=" & UCase(dm.Q(Trim(Add2_txt.Text))) & _
                ", city_cd=" & UCase(dm.Q(Trim(City_cmb.SelectedValue))) & _
                ", country_cd=" & UCase(dm.Q(Trim(Country_cmb.SelectedValue))) & _
                ", zip_cd=" & UCase(dm.Q(Trim(Zip_txt.Text))) & _
                ", tel_no=" & UCase(dm.Q(Trim(T1_txt.Text))) & _
                ", fax_no=" & UCase(dm.Q(Trim(F1_txt.Text))) & _
                ", email_address=" & UCase(dm.Q(Trim(Email_txt.Text))) & _
                ", remarks=" & UCase(dm.Q(Trim(Remarks_txt.Text))) & _
                ", user_last_upd_by='" & UCase(Session("UserName")) & "', user_last_upd_dt='" & _
                Now & "'" & _
                " WHERE user_name='" & UCase(Trim(Session("user_cd").ToString)) & "'")

        ElseIf Session("user_mode") = "Add" Then
            'must have city, country, start_date, and name

            'fix this
            zClass.LookUpCode = "ADD_USER"
            If zClass.DuplicateCode(Trim(Name_txt.Text)) = False Then Exit Sub

            Dim pfunc As New PolyFunc.MiscFunc

            Dim strPass As String = UCase(pfunc.RandomID(6))


            zClass.ExecQuery("INSERT INTO user_mstr(user_name, user_last_name, user_first_name, user_middle_name, dept_cd, role_cd, advisor_tag, advisor_level_cd, " & _
                "user_address, user_address2, city_cd, country_cd, zip_cd, tel_no, fax_no, mobile_no, " & _
                "email_address, remarks, user_crea_by, user_crea_dt,user_password) " & _
                "VALUES(" & UCase(dm.Q(Trim(Name_txt.Text))) & _
                "," & UCase(dm.Q(Trim(LastName_txt.Text))) & _
                "," & UCase(dm.Q(Trim(FirstName_txt.Text))) & _
                "," & UCase(dm.Q(Trim(MiddleName_txt.Text))) & _
                ",'" & Trim(Dept_cmb.SelectedValue) & "'" & _
                ",'" & Trim(Role_cmb.SelectedValue) & "'" & _
                "," & CInt(Advisor_chk.Checked) & _
                ",'" & Trim(Level_cmb.SelectedValue) & "'" & _
                "," & UCase(dm.Q(Trim(Add1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(Add2_txt.Text))) & _
                "," & UCase(dm.Q(Trim(City_cmb.SelectedValue))) & _
                "," & UCase(dm.Q(Trim(Country_cmb.SelectedValue))) & _
                "," & UCase(dm.Q(Trim(Zip_txt.Text))) & _
                "," & UCase(dm.Q(Trim(T1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(F1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(M1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(Email_txt.Text))) & _
                "," & UCase(dm.Q(Trim(Remarks_txt.Text))) & _
                ",'" & Session("UserName") & "'" & _
                ",'" & Now & "'" & _
                "," & UCase(dm.Q(strPass)) & ")")
            Session("user_cd") = Trim(Name_txt.Text)

            'TODO: Uncomment for email notification
            Dim x As New SmtpClient
            Dim msg As String

            msg = "Your Username is : " & Name_txt.Text & vbCrLf & _
                    "Your Password is : " & strPass & vbCrLf & vbCrLf & _
                    "Do not reply to this email."

            Dim cfunc As New CIMSfunc

            Try
                With x
                    .Host = cfunc.GetSMTPServer()
                    .Port = 25
                    .UseDefaultCredentials = True
                    x.Send("CIMS Administrator <CIMS-Admin@No-Reply.com>", Me.Email_txt.Text, "CIMS Account Details", msg)
                End With
            Catch ex As Exception
                System.Diagnostics.Debug.Print(ex.Message.ToString)
            End Try
            

            cfunc = Nothing
            x = Nothing
        End If

        Session.Remove("user_mode") : Session.Remove("user_cd")
        Response.Redirect("~/Admin/System_Users.aspx")
    End Sub

    Protected Sub Advisor_chk_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Advisor_chk.CheckedChanged
        If Advisor_chk.Checked Then
            Level_cmb.Enabled = True
        Else
            Level_cmb.Enabled = False
        End If
    End Sub
End Class